package com.yicun.road.service.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yicun.road.service.pojo.api.CommonResult;
import com.yicun.road.service.pojo.base.ResultCode;
import com.yicun.road.service.pojo.base.RspInfo;
import com.yicun.road.service.pojo.base.RspPageInfo;
import com.yicun.road.service.pojo.disease.TDefectsPits;
import com.yicun.road.service.service.DiseaseService;
import com.yicun.road.service.util.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 病害信息管理控制器
 */
@RestController
@CrossOrigin
@RequestMapping("/disease")
@Api(tags = "TDefectsPits-相关接口")
public class DiseaseInfoController {

    @Resource
    private DiseaseService diseaseService;

    /**
     * 查询病害信息列表
     *
     * @param tDefectsPits
     * @return
     */
    @PostMapping("/queryDiseaseList")
    @ApiOperation(value = "查询病害信息列表(带分页)")
    public RspPageInfo<List<TDefectsPits>> queryTDefectsPitsList(@RequestBody TDefectsPits tDefectsPits) {
        try {
            Page<TDefectsPits> page = diseaseService.queryTDefectsPitsList(tDefectsPits);
            return ResponseUtil.makePageRsp(ResultCode.SUCCESS.CODE, page.getTotal(), "", page.getRecords());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.makePageRsp(ResultCode.FAIL.CODE, 0, "系统异常:" + e.getMessage(), null);
        }
    }

    /**
     * 新增病害信息列表
     *
     * @param tDefectsPits
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增")
    public CommonResult saveTDiseasePits(@RequestBody TDefectsPits tDefectsPits) {
        diseaseService.insertTDefectsPits(tDefectsPits);
        return CommonResult.success();
    }

    /**
     * 修改病害信息列表
     *
     * @param tDefectsPits
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改")
    public CommonResult updateTDiseasePits(@RequestBody TDefectsPits tDefectsPits) {
        diseaseService.updateTDiseasePits(tDefectsPits);
        return CommonResult.success();
    }

    /**
     * 删除病害信息列表
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除")
    public CommonResult deleteTDiseasePits(@PathVariable Integer id) {
        diseaseService.deleteTDiseasePits(id);
        return CommonResult.success();
    }

}
